package com.ltv.xiangqiplugin.views;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JFileChooser;

import com.ltv.xiangqiplugin.models.JChessFileFilter;

@SuppressWarnings("serial")
public class SaveLoadFileDialog extends JFileChooser {
	private static SaveLoadFileDialog instance;
	
	private String fileType = JChessFileFilter.FILE_TYPE_PGN;
	
	private JChessFileFilter pgnFilter;
	private JChessFileFilter allFilter;
	
	private SaveLoadFileDialog() {
		super();
		setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		setAcceptAllFileFilterUsed(false);
		
		pgnFilter = new JChessFileFilter(".pgn", "Portable Game Notation (*.pgn)");
		allFilter = new JChessFileFilter(".*", "All Files (*.*)");
		
	    addChoosableFileFilter(pgnFilter);
	    addChoosableFileFilter(allFilter);
	    
	    setFileFilter(pgnFilter);
	    
	    addPropertyChangeListener(JFileChooser.FILE_FILTER_CHANGED_PROPERTY, new PropertyChangeListener() {
			
			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if (getFileFilter() == pgnFilter) {
					fileType = JChessFileFilter.FILE_TYPE_PGN;
				} else if (getFileFilter() == allFilter) {
					fileType = JChessFileFilter.FILE_TYPE_ALL;
				}
			}
		});
	}
	
	public static SaveLoadFileDialog getInstance() {
		if (instance == null) {
			instance = new SaveLoadFileDialog();
		}
		
		return instance;
	}

	public String getFileType() {
		return fileType;
	}
	
}
